package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.RescueStation;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.RescueStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("rescueStation")
@Api(description = "救助站管理接口")
public class RescueStationController {

    @Autowired
    RescueStationService rescueStationService;


    //模糊查询 （救助站名称，地址）+ 分页
    @GetMapping("/listRS")
    @ApiOperation(value = "本接口按条件分页查询救助站")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照救助站名称条件查询", dataType = "String", paramType = "query", name = "name"),
            @ApiImplicitParam(value = "按照地址模糊查询条件", dataType = "String", paramType = "query", name = "address")
    })
    public ResultResponse<PageInfo<RescueStation>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name, String address) {

        ResultResponse<PageInfo<RescueStation>> result = null;
        try {
            PageInfo<RescueStation> pageInfo = rescueStationService.queryRSByPage(now, size, name, address);
            result = new ResultResponse<>(200, "查询救助站成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询救助站失败!");
        }
        return result;
    }


    //模糊查询 （救助站名称，地址）+ 分页
    @GetMapping("/listOldRS")
    @ApiOperation(value = "本接口按条件分页查询历史救助站")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照救助站名称条件查询", dataType = "String", paramType = "query", name = "name"),
            @ApiImplicitParam(value = "按照地址模糊查询条件", dataType = "String", paramType = "query", name = "address")
    })
    public ResultResponse<PageInfo<RescueStation>> oldList(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name, String address) {

        ResultResponse<PageInfo<RescueStation>> result = null;

        try {
            PageInfo<RescueStation> pageInfo = rescueStationService.queryOldRSByPage(now, size, name, address);
            result = new ResultResponse<>(200, "查询历史救助站成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询历史救助站失败!");
        }
        return result;
    }

    //修改返显
    @GetMapping("/getRSByKey/{id}")
    @ApiOperation(value = "本接口按id查询救助站数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "按照id查询救助站", dataType = "int", paramType = "query", name = "id")
    })
    public ResultResponse<RescueStation> getRSByKey(@PathVariable("id") int id) {

        ResultResponse<RescueStation> result = null;
        try {
            RescueStation rescueStation = rescueStationService.selectRSByKey(id);
            result = new ResultResponse<>(200, "查询历史救助站成功!", rescueStation);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询历史救助站失败!");
        }
        return result;
    }


    @PostMapping("/addRS")  //添加用  PostMapping
    @ApiOperation(value = "添加救助站")
    public ResultResponse<Void> addRS(@RequestBody RescueStation rescueStation) {
        ResultResponse<Void> result = null;
        try {
            rescueStation.setDel(0);
            rescueStation.setInsertTime(new Date());
            rescueStationService.insertRS(rescueStation);
            result = new ResultResponse<>(200, "添加救助站成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加救助站失败！");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据救助站的标识ID 逻辑删除救助站")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "救助站的ID标识")
    })
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;

        try {
            int i = rescueStationService.removeRS(id);
            if (i == 1) {
                result = new ResultResponse<>(200, "注销救助站成功!");
//                result = new ResultResponse<>(200, "删除救助站成功!");
            } else {
//                result = new ResultResponse<>(201, "删除救助站失败,当前救助站还有宠物未转移，请转移之后再删除!");
                result = new ResultResponse<>(201, "注销救助站失败!");
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "系统错误，请联系管理员!");
        }
        return result;
    }

    @DeleteMapping("/trueDel/{id}")
    @ApiOperation(value = "根据救助站的标识ID 真删救助站")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "救助站的ID标识")
    })
    public ResultResponse<Void> trueDel(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;

        try {
            int i = rescueStationService.removeRS2(id);
            if (i == 1) {
                result = new ResultResponse<>(200, "删除救助站成功!");
            } else {
                result = new ResultResponse<>(201, "删除救助站失败,当前救助站还有宠物未转移，请转移之后再删除!");
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "系统错误，请联系管理员!");
        }
        return result;
    }


    @PutMapping("/modify")
    @ApiOperation(value = "修改救助站的信息")
    public ResultResponse<Void> modify(@RequestBody RescueStation rescueStation) {
        ResultResponse<Void> result = null;
        try {

            rescueStation.setUpdateTime(new Date());
            rescueStationService.updateRS(rescueStation);
            result = new ResultResponse<>(200, "救助站修改成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "救助站修改失败！");
        }
        return result;
    }


}
